package com.twu.biblioteca;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;


/**
 * Created with IntelliJ IDEA.
 * User: Deepika
 * Date: 5/13/13
 * Time: 6:53 PM
 * To change this template use File | Settings | File Templates.
 */

import static javax.swing.JOptionPane.showInputDialog;
import static javax.swing.JOptionPane.showMessageDialog;


public class Biblioteca{

    private JPanel panel1;
    private JToolBar ToolBar;
    private JButton viewBooksInLibraryButton;
    private JButton reserveABookButton;

    private JButton reserveButton;
    private JButton membershipDetailsButton;
    private JButton viewMoviesButton;
    private JButton loginButton;
    JTable table1;

    Login l = new Login();

    BibliotecaLibrary bibliotecaLibrary = new BibliotecaLibrary();


    public void bibliotecaActionListeners(){

    viewBooksInLibraryButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            viewBooksButtonClick();
        }
    });

    reserveABookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reserveBookButtonClick();
            }
        });

    membershipDetailsButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                membershipButtonClick();
            }
        });

    reserveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                reserveButtonClick();
            }
        });

    viewMoviesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                viewMoviesButtonClick();
            }
        });

    loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginButtonClick();
            }
        });


    }

    public void addBooksToTable() {

        DefaultTableModel tableModel = new DefaultTableModel(0,0);
        ArrayList<Book> books = new ArrayList<Book>();
        String header[] = new String[]{"Books"};
        tableModel.setColumnIdentifiers(header);
        table1.setModel(tableModel);

        books = bibliotecaLibrary.getAllBooks();

        for(Book b: books){
            Vector v = new Vector();
            v.add(b.getName());
            tableModel.addRow(v);
        }

        table1.setVisible(true);
    }

    public void addMoviesToTable() {
        DefaultTableModel tableModel = new DefaultTableModel(0,0);
        ArrayList<Movies> moviesList = new ArrayList<Movies>();
        String header[] = new String[]{"Movie","Year","Director","Rating"};
        tableModel.setColumnIdentifiers(header);
        table1.setModel(tableModel);


        Vector add_header = new Vector();
        add_header.add("Movie");
        add_header.add("Year");
        add_header.add("Director");
        add_header.add("Rating");
        tableModel.addRow(add_header);

        moviesList = bibliotecaLibrary.getAllMovies();

        for(Movies m: moviesList){
            Vector v = new Vector();
            v.add(m.getName());
            v.add(m.getYear());
            v.add(m.getDirector());
            v.add(m.getRating());
            tableModel.addRow(v);
        }

        table1.setVisible(true);
    }

    public void viewBooksButtonClick(){
        addBooksToTable();
        table1.setEnabled(false);
        reserveButton.setVisible(false);
    }

    public void reserveBookButtonClick(){
        if(l.getLogin(l.getUsername()).equals(false)){
            showMessageDialog(null, "Please log in to use this facility.");
        }
        else {
            addBooksToTable();
            table1.setEnabled(true);
            reserveButton.setVisible(true);
        }
    }

    public void membershipButtonClick(){
        if(l.getLogin(l.getUsername()).equals(false)){
            showMessageDialog(null, "Please talk to a Librarian. Thank you.");
        }
        else{
            ArrayList<User> usersList = new ArrayList<User>();
            usersList = bibliotecaLibrary.getAllUsers();
            for (User u:usersList){
                if(u.getUsername().equals(l.getUsername())){
                    showMessageDialog(null, u.getName() + " " + u.getNumber() + " " + u.getEmail());
                }
            }
        }
    }

    public void reserveButtonClick(){
        Integer index = table1.getSelectedRowCount();
        bibliotecaLibrary.reserveBooks(index);
    }

    public void viewMoviesButtonClick(){
        addMoviesToTable();
        table1.setEnabled(false);
        reserveButton.setVisible(false);
    }

    public void loginButtonClick(){
        JTextField enterUsername = new JTextField(10);
        JTextField enterPassword = new JTextField(10);

        JPanel myPanel = new JPanel();
        GridLayout layout = new GridLayout(2, 1);
        layout.setVgap(5);
        myPanel.setLayout(layout);

        myPanel.add(new JLabel("Username:"));
        myPanel.add(enterUsername);

        myPanel.add(new JLabel("Password:"));
        myPanel.add(enterPassword);

        int result = JOptionPane.showConfirmDialog(null, myPanel,
                "Please Enter Username and Password", JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            Boolean check = bibliotecaLibrary.checkUser(enterUsername.getText(), enterPassword.getText());
            if (check.equals(true)){
                l.setLogin(enterUsername.getText());
                showMessageDialog(null, "Welcome " + enterUsername.getText() + " . You are now Logged in!");
            }
            else
                showMessageDialog(null,"Incorrect Details!");
        }
    }



    public Biblioteca() {

        //do not display the reserve button initially
        reserveButton.setVisible(false);
        table1.setVisible(false);

         bibliotecaActionListeners();
    }


    public static void main(String[] args) {

        JFrame frame = new JFrame("Welcome to Biblioteca");
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setLocation(300,300);
        frame.setContentPane(new Biblioteca().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.isResizable();
        frame.pack();
        showMessageDialog(null, "Welcome to Biblioteca!");

    }

}
